package com.yunxin.core.domain.service.order.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.yunxin.core.application.admin.team.DistributeDo;
import com.yunxin.core.application.command.order.OperateAllocationOrderRest;
import com.yunxin.core.domain.model.order.OrderAllocationBelongToAccount;
import com.yunxin.core.domain.model.order.OrderAllocationBelongToAccountRepository;
import com.yunxin.core.domain.model.order.OrderUserApplyAllocation;
import com.yunxin.core.domain.model.order.OrderUserApplyAllocationRepository;
import com.yunxin.core.domain.service.order.OrderUserApplyAllocationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author zero°
 * @since 2023-06-07
 */
@DS("core")
@Slf4j
@Service
@RequiredArgsConstructor
public class OrderUserApplyAllocationServiceImpl implements OrderUserApplyAllocationService {

    private final OrderAllocationBelongToAccountRepository orderAllocationBelongToAccountRepository;

    private final OrderUserApplyAllocationRepository orderUserApplyAllocationRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int operateAllocationOrderAccount(OrderUserApplyAllocation allocation, Long accountId, String operator) {
        Long currentAccount = 0L;
        // 账户id为空,默认解除分配
        if (Objects.nonNull(accountId)) {
            currentAccount = accountId;
        }
        Date date = new Date();
        // 查询有效的归属账户（有且只有一条）,逻辑删除
        OrderAllocationBelongToAccount orderAllocationBelongToAccountDO = orderAllocationBelongToAccountRepository.getOrderAllocationBelongToAccountByAllocationOrderId(allocation.getId());
        orderAllocationBelongToAccountDO.setBelongToEndTime(date);
        orderAllocationBelongToAccountDO.setIsDel(1);
        orderAllocationBelongToAccountDO.setEditor(operator);
        orderAllocationBelongToAccountDO.setEditTime(date);
        orderAllocationBelongToAccountDO.setVersion(orderAllocationBelongToAccountDO.getVersion() + 1);
        orderAllocationBelongToAccountRepository.updateOrderAllocationBelongToAccount(orderAllocationBelongToAccountDO);
        // 新建新的归属账户
        OrderAllocationBelongToAccount allocationBelongToAccount = new OrderAllocationBelongToAccount();
        allocationBelongToAccount.setBelongToStartTime(date);
        allocationBelongToAccount.setCreator(operator);
        allocationBelongToAccount.setAllocationOrderId(allocation.getId());
        allocationBelongToAccount.setOriginalAccountId(allocation.getCurrentAccountId());
        allocationBelongToAccount.setCurrentAccountId(currentAccount);
        orderAllocationBelongToAccountRepository.saveOrderAllocationBelongToAccount(allocationBelongToAccount);
        // 更新订单的归属账户
        allocation.setCurrentAccountId(currentAccount);
        allocation.setEditor(operator);
        allocation.setEditTime(date);
        allocation.setVersion(allocation.getVersion() + 1);
        return orderUserApplyAllocationRepository.updateOrderUserApplyAllocation(allocation);
    }


    /**
     * 查询当前商户信息
     *
     * @param userId
     * @return
     */
    @Override
    public Long merchantIdByUserId(Long userId) {
        return orderUserApplyAllocationRepository.merchantIdByUserId(userId);
    }


    /**
     * 保存任务i
     *
     * @param distributeDo
     * @return
     */
    @Override
    public Integer saveTeamTask(DistributeDo distributeDo) {
        return orderUserApplyAllocationRepository.saveTeamTask(distributeDo);
    }


    /**
     * 根据订单id查询订单信息
     *
     * @param orderIds
     * @return
     */
    @Override
    public List<OrderUserApplyAllocation> selectOrderDetail(List<Long> orderIds) {
        return orderUserApplyAllocationRepository.selectOrderDetail(orderIds);
    }

    /**
     * 校验订单是否被分配
     *
     * @param orderIds
     * @return
     */
    @Override
    public List<Integer> selectalreadyDistribute(List<Long> orderIds) {
        return orderUserApplyAllocationRepository.selectalreadyDistribute(orderIds);
    }
}
